package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.config.ItemFrignConfig;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
//

//    分页查询商品
    @GetMapping("/list")
    public PageDTO<Item> IpageController(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        Page<Item> page1 = itemService.page(new Page<Item>(page, size));
        page1.setOptimizeCountSql(true);
        return new PageDTO<Item>(page1.getTotal(),page1.getRecords());
    }
//    根据Id查询
    @GetMapping("/{id}")
    public Item getByIdController(@PathVariable("id")Long id){
        if(id==null){
            throw new  RuntimeException();
        }
        Item byId = itemService.getById(id);
        return byId;
    }
//    新增商品
    @PostMapping
    public void postController(@RequestBody Item item){
        String name = item.getName();
        if(item==null&&"".equals(item)){
            throw new RuntimeException();
        }
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);

    }
//    商品上下架
    @Transactional
    @PutMapping("/status/{id}/{status}")
    public void putByIdOrStatus(@PathVariable("id")Long id,@PathVariable("status") Integer status){
        if(id==null&&status==null){
            throw new RuntimeException();
        }
        Item item = itemService.getById(id);
        if(StringUtils.isEmpty(item)){
            throw new RuntimeException();
        }
        if(status==2){
            item.setStatus(2);
        }
        if(status==1){
            item.setStatus(1);
        }
        String routingKey=status==2?"hamll1.delete.fanout":"hmall1.insert.fanout";
        rabbitTemplate.convertAndSend("hmall1.topic",routingKey,id);
        itemService.updateById(item);

    }
//    修改商品信息
    @PutMapping
    public void putController(@RequestBody Item item){
        if(StringUtils.isEmpty(item)){
            throw new RuntimeException();
        }
        if(item.getStatus()==1){
            item.setStatus(2);
            itemService.updateById(item);
        }
        itemService.updateById(item);
    }
//    根据id删除
    @DeleteMapping("/{id}")
    public void deleteController(@PathVariable Long id){
        Item item = itemService.getById(id);
        if(item.getStatus()==1){
            item.setStatus(2);
            itemService.updateById(item);
        }
        itemService.removeById(id);
    }
//    减库存
    @PutMapping("/stock/{itemId}/{num}")
    public void updateByItemId(@PathVariable("itemId")Long itemId,@PathVariable("num")Integer num){
        itemService.updateByNum(itemId,num);
    }
}
